package qst.com.bacK.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import qst.com.bacK.Constant;
import qst.com.bacK.utils.RequestUtil;
import qst.com.bacK.utils.WebUtil;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.pojo.entity.Team;
import qst.com.bacK.pojo.entity.User;
import qst.com.bacK.service.ClassTeamService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author:   msh
 * Date:     2024/10/12 上午9:53
 * Description:
 */

@Controller
@RequestMapping("/team")
public class TeamController {
    @Autowired
    private ClassTeamService classTeamService;

    // 创建
    @RequestMapping("/create")
    public void create(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Team team = new Team();
        team.setStatus(1);
        try {
            req.setAttribute("team", team);
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
        }
        WebUtil.forward(req, resp, "/team/create.jsp");
    }

    // delete
    @RequestMapping("/delete.action")
    public void delete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = RequestUtil.getInt(req, "id");
        try {
            classTeamService.deleteTeam(id);
            req.getSession().setAttribute("_message_key", "批次已删除");
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
        }
        resp.sendRedirect("/team/list.action");
    }

    // 编辑
    @RequestMapping("/edit.action")
    public void edit(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 获取要查看的班级的id
        Integer id = RequestUtil.getInt(req, "id");
        try {
            req.setAttribute("team", classTeamService.findById(id));
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
        }
        WebUtil.forward(req, resp, "/team/edit.jsp");
    }

    //列表
    @RequestMapping("/list.action")
    public void list(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 获取当前登录的用户
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER);
        try {
            if (user.getType() == 1) {
                req.setAttribute("teamList", classTeamService.findAll());
//                model.addAttribute("teamList", classTeamService.findAll());
            } else {
                req.setAttribute("teamList",
                        classTeamService.findByCreator(user.getId()));
            }

        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
        }
        WebUtil.forward(req, resp, "/team/list.jsp");
    }

    // 保存
    @RequestMapping("/save.action")
    public void save(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 将表单数据组装为ClassTeam对象
        Team t = new Team();
        t.setName(RequestUtil.getString(req, "name"));
        t.setBeginYear(RequestUtil.getDate(req, "beginYear"));
        t.setStatus(RequestUtil.getInt(req, "status"));
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER);
        t.setCreatorId(user.getId());// 设置创建者
        try {
            classTeamService.saveTeam(t);
            resp.sendRedirect("/team/view.action?id=" + t.getId());
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
        }
        req.setAttribute("team", t);
        WebUtil.forward(req, resp, "/team/create.jsp");
    }
    // 更新
    @RequestMapping("/update.action")
    public void update(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 将表单数据组装为ClassTeam对象
        Team t = new Team();
        t.setId(RequestUtil.getInt(req, "id"));
        t.setName(RequestUtil.getString(req, "name"));
        t.setBeginYear(RequestUtil.getDate(req, "beginYear"));
        t.setStatus(RequestUtil.getInt(req, "status"));
        try {
            classTeamService.updateTeam(t);
            resp.sendRedirect("/team/view.action?id=" + t.getId());
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
            req.setAttribute("team", t);
            WebUtil.forward(req, resp, "/team/edit.jsp");
        }
    }

    // view
    @RequestMapping("/view.action")
    public void viewTeam(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 获取要查看的班级的id
        Integer id = RequestUtil.getInt(req, "id");
        try {
            req.setAttribute("team", classTeamService.findById(id));
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
        }
        WebUtil.forward(req, resp, "/team/view.jsp");
    }
}
